package com.itheima.demo01;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class SendMessage {
    public static void main(String[] args) throws IOException {
        //进行数据的发送
        /*
        用于接受数据
        当从键盘输入886时就停止程序
         */

        //1.创建对象
        DatagramSocket ds = new DatagramSocket();

        //2.进行数据的打包
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要发送的数据");
            String str = sc.nextLine();
            byte[] bytes = str.getBytes();

            //判断退出条件886
            if("886".equals(str)){
                break;
            }
            InetAddress address = InetAddress.getByName("127.0.0.1");
            int port = 10086;
            DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
            //3.发送数据
            ds.send(dp);
        }

        //4.进行关流
        ds.close();
    }
}
